﻿using System;
using System.IO;
using System.Text;
using System.Xml.Serialization;

namespace MyFloridaHouse.libSvKMZ.KML
{
    /// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1")]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
    public partial class kml
    {

        private kmlDocument[] itemsField;

        /// <remarks/>
        [System.Xml.Serialization.XmlElementAttribute("Document", Form = System.Xml.Schema.XmlSchemaForm.Unqualified)]
        public kmlDocument[] Items
        {
            get
            {
                return this.itemsField;
            }
            set
            {
                this.itemsField = value;
            }
        }

        public static MemoryStream Serialize(kml kmlToSerialize)
        {
            XmlSerializer mySerializer = new XmlSerializer(typeof(kml));
            MemoryStream kmlStream = new MemoryStream();
            mySerializer.Serialize( kmlStream, kmlToSerialize);

            // now replace the XML serialize header with the Google serialize header
            kmlStream.Seek(0, 0);
            StreamReader sr = new StreamReader(kmlStream);
            MemoryStream outKmlStream = new MemoryStream();
            StreamWriter sw = new StreamWriter(outKmlStream);

            string outString;
            int i = 1;
            while (!sr.EndOfStream)
            {
                
                outString = sr.ReadLine();
                outString = outString.Replace("_x003A_", ":");  // XML changes the ':' symbol to _x003A_, need to change it  back
                outString = System.Windows.Browser.HttpUtility.HtmlDecode(outString);
                if (i == 2)
                {
                    //Because the XmlSerializer seems to have trouble w/ the kml namespace we need to swap it w/ 
                    //standard xml namesspace as appropriate
                    // take out this line: <kml xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
                    outString = @"<kml xmlns=""http://earth.google.com/kml/2.0"" xmlns:atom=""http://www.w3.org/2005/Atom"">";
                }
                i++;
                sw.WriteLine(outString);
            }

            sw.Flush();
            return outKmlStream;
        }

        public static kml Deserialize(MemoryStream memStreamKML)
        {
            kml kmlDeserialized;
            XmlSerializer mySerializer = new XmlSerializer(typeof(kml));

            StreamReader sr = new StreamReader(memStreamKML);

            String outString;
            StringBuilder sb = new StringBuilder();
            int i = 1;
            while (!sr.EndOfStream)
            {
                outString = sr.ReadLine();
                outString = outString.Replace(@"\", String.Empty);
                if (i == 2)
                {
                    //Because the XmlSerializer seems to have trouble w/ the kml namespace we need to swap it w/ 
                    //standard xml namesspace as appropriate
                    //<kml xmlns="http://earth.google.com/kml/2.0"> 
                    outString = @"<kml xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd=""http://www.w3.org/2001/XMLSchema"">";
                }
                i++;
                if (!outString.Contains("atom:"))  //deserializer can't handle atom namespace, we don't need any info on it
                {
                    sb.Append(outString);
                }
            }
            byte[] byteArray = (new UTF8Encoding()).GetBytes(sb.ToString().Replace(@"\", String.Empty));
            MemoryStream newStream = new MemoryStream(byteArray);
            kmlDeserialized = (kml)mySerializer.Deserialize(newStream);
            return kmlDeserialized;

        }
    }
}
